package com.glsc.ngateway.platform.controller.cmdb;

import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDiskStandard;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.service.cmdb.CmdbDiskStandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.glsc.ngateway.common.api.common.enums.DictConstant.CMDB_DISK_STANDARD;
import static com.glsc.ngateway.common.api.common.enums.DictConstant.CMDB_WEB;

@RestController
@Api(value = "磁盘标准管理", tags = {"磁盘标准管理"})
public class CmdbDiskStandardController {

    @Autowired
    private CmdbDiskStandardService cmdbDiskStandardService;
    @Autowired
    private UserCache userCache;
    @Autowired
    private HttpServletRequest request;

    /**
     * 查询磁盘标准
     * @param params
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/diskStandard/search")
    @ApiOperation(value = "查询磁盘标准", notes = "查询磁盘标准")
    public PlatformResponse<Object> searchDiskStandard(@RequestBody Object params) {
        Object result = cmdbDiskStandardService.search(CMDB_DISK_STANDARD, params);
        return PlatformResponse.successData(result);
    }

    @RequestMapping(value = "/diskStandard/findByStandardCode")
    @ApiOperation(value = "通过standardCode查询", notes = "通过standardCode查询")
    public PlatformResponse<Object> findByStandardCode(@RequestParam String diskStandardCode) {
        Object result = cmdbDiskStandardService.findByStandardCode(diskStandardCode);
        return PlatformResponse.successData(result);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/diskStandard/addOrUpdate")
    @ApiOperation(value = "修改磁盘标准", notes = "修改磁盘标准")
    public PlatformResponse<Void> addOrUpdateDiskStandard(@RequestBody CmdbDiskStandard cmdbDiskStandard) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbDiskStandardService.addOrUpdateDiskStandard(cmdbDiskStandard, account,CMDB_WEB);
        return PlatformResponse.successMsg("处理成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "/diskStandard/delete")
    @ApiOperation(value = "删除磁盘标准", notes = "删除磁盘标准")
    public PlatformResponse<Void> deleteDiskStandard(@RequestBody CmdbDiskStandard cmdbDiskStandard) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbDiskStandardService.deleteDiskStandard(cmdbDiskStandard, account);
        return PlatformResponse.successMsg("删除成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "/diskStandard/deleteList")
    @ApiOperation(value = "批量删除磁盘标准", notes = "批量删除磁盘标准")
    public PlatformResponse<Void> deleteListDiskStandard(@RequestBody List<String> codes) {
        PlatformUserDto user = userCache.get(request);
        String account = user.getUsername();
        cmdbDiskStandardService.deleteByCodeList(codes, account);
        return PlatformResponse.successMsg("删除成功");
    }
}
